Running a Downloaded Image
Launching Containers from Images
Launching containers from images is a fundamental part of working with Docker. To do this, you use the docker run command, which creates and starts a new container based on a specified image.
The general syntax for this command is:
docker run [OPTIONS] IMAGE [COMMAND]
Some of the most common options include:
-d: Runs the container in detached mode (in the background);-p: Maps ports between your host and the container;--name: Assigns a custom name to your container;-it: Runs the container interactively, allowing you to use a shell inside the container.
Running the following command starts a new Ubuntu container and provides an interactive shell inside it:
docker run -p 8080:80 nginx
The -p flag maps the containerβs internal port 80 to port 8080 on your local machine.
Once the container is running, you can open your browser and go to:
http://localhost:8080
You will see Nginx running at this address. Notice that nothing is running directly on your computerβeverything is running inside Docker.
Container Lifecycle: Starting, Stopping, and Removing Containers
Once a container has been started, it goes through a lifecycle that includes several key stages:
- The container starts when you run it from an image;
- It remains running as long as its main process is active;
- You can stop a running container with the
docker stopcommand, which gracefully shuts down the container's process; - If you want to remove a container entirely, use
docker rm, which deletes the container from your system.
Removing a container does not remove the underlying image, so you can always launch a new container from the same image later. Understanding these basic lifecycle commandsβstarting, stopping, and removing containersβwill help you manage your Docker environment efficiently as you build and test applications.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 7.14
Running a Downloaded Image
Swipe to show menu
Launching Containers from Images
Launching containers from images is a fundamental part of working with Docker. To do this, you use the docker run command, which creates and starts a new container based on a specified image.
The general syntax for this command is:
docker run [OPTIONS] IMAGE [COMMAND]
Some of the most common options include:
-d: Runs the container in detached mode (in the background);-p: Maps ports between your host and the container;--name: Assigns a custom name to your container;-it: Runs the container interactively, allowing you to use a shell inside the container.
Running the following command starts a new Ubuntu container and provides an interactive shell inside it:
docker run -p 8080:80 nginx
The -p flag maps the containerβs internal port 80 to port 8080 on your local machine.
Once the container is running, you can open your browser and go to:
http://localhost:8080
You will see Nginx running at this address. Notice that nothing is running directly on your computerβeverything is running inside Docker.
Container Lifecycle: Starting, Stopping, and Removing Containers
Once a container has been started, it goes through a lifecycle that includes several key stages:
- The container starts when you run it from an image;
- It remains running as long as its main process is active;
- You can stop a running container with the
docker stopcommand, which gracefully shuts down the container's process; - If you want to remove a container entirely, use
docker rm, which deletes the container from your system.
Removing a container does not remove the underlying image, so you can always launch a new container from the same image later. Understanding these basic lifecycle commandsβstarting, stopping, and removing containersβwill help you manage your Docker environment efficiently as you build and test applications.
Thanks for your feedback!