Create and stop some containers
In this step, we will learn how to create and stop Docker containers. Docker containers are lightweight, portable, and self-sufficient units that contain everything needed to run an application.
First, let's pull a Docker image that we will use to create containers. We will use the ubuntu
image, which is a minimal Ubuntu operating system.
docker pull ubuntu
You should see output indicating that the image is being downloaded.
Using default tag: latest
latest: Pulling from library/ubuntu
...
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
Now that we have the image, let's create and run a container from it. We will use the docker run
command. The -d
flag runs the container in detached mode (in the background), and the --name
flag assigns a name to the container.
docker run -d --name my-ubuntu-container ubuntu sleep infinity
This command creates a container named my-ubuntu-container
from the ubuntu
image and runs the sleep infinity
command inside it. The sleep infinity
command keeps the container running indefinitely.
You should see a long string of characters as output, which is the container ID.
<container_id>
To verify that the container is running, we can use the docker ps
command.
docker ps
This command lists all running containers. You should see your my-ubuntu-container
listed.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<container_id> ubuntu "sleep infinity" X seconds ago Up X seconds my-ubuntu-container
Now, let's create another container, but this time we won't give it a specific name. Docker will automatically generate a name for it.
docker run -d ubuntu sleep infinity
Again, you will see the container ID as output.
<container_id>
Run docker ps
again to see both running containers.
docker ps
You should now see two containers listed.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<container_id> ubuntu "sleep infinity" X seconds ago Up X seconds my-ubuntu-container
<container_id> ubuntu "sleep infinity" X seconds ago Up X seconds <random_name>
Now, let's stop the container named my-ubuntu-container
. We can use the docker stop
command followed by the container name or ID.
docker stop my-ubuntu-container
You should see the container name as output, indicating that it has been stopped.
my-ubuntu-container
Run docker ps
again. You will see that my-ubuntu-container
is no longer listed, as docker ps
only shows running containers.
docker ps
To see all containers, including stopped ones, use the docker ps -a
command.
docker ps -a
You should now see both containers, with my-ubuntu-container
having a status of Exited
.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<container_id> ubuntu "sleep infinity" X minutes ago Exited (0) X seconds ago my-ubuntu-container
<container_id> ubuntu "sleep infinity" X minutes ago Up X minutes <random_name>
Finally, let's stop the second container using its container ID. You can get the ID from the output of docker ps -a
. Replace <container_id>
with the actual ID of the second container.
docker stop <container_id>
You should see the container ID as output.
<container_id>
Run docker ps -a
one last time to confirm that both containers are stopped.
docker ps -a
Both containers should now have a status of Exited
.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<container_id> ubuntu "sleep infinity" X minutes ago Exited (0) X seconds ago my-ubuntu-container
<container_id> ubuntu "sleep infinity" X minutes ago Exited (0) X seconds ago <random_name>