Create and stop a container
In this step, you will learn how to create and stop a Docker container. A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI.
First, let's pull the hello-world
image from Docker Hub. This is a very small image that is useful for testing.
docker pull hello-world
You should see output indicating that the image is being pulled and extracted.
Using default tag: latest
latest: Pulling from library/hello-world
...
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
Now, let's create and run a container from the hello-world
image. When you run this command, Docker will create a new container and execute the command specified in the image. In the case of hello-world
, the command simply prints a message and then exits.
docker run hello-world
You should see output similar to this:
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
This output confirms that the container ran successfully and printed its message. Since the command in the container finished, the container has stopped.
To see the containers on your system, including those that have stopped, you can use the docker ps -a
command.
docker ps -a
You will see a list of containers. The hello-world
container should be in the list, and its status should be Exited
.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
... hello-world "/hello" About a minute ago Exited (0) 58 seconds ago ...
Now, let's create another container, but this time we will use the ubuntu
image and run a command that keeps the container running for a short period. We will use the sleep
command to keep the container alive for 10 seconds.
First, pull the ubuntu
image:
docker pull ubuntu
You should see output indicating the image is being pulled.
Using default tag: latest
latest: Pulling from library/ubuntu
...
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
Now, run a container from the ubuntu
image and execute the sleep 10
command. We will run this in detached mode (-d
) so that the container runs in the background and doesn't block our terminal.
docker run -d ubuntu sleep 10
This command will output the container ID.
[container_id]
Use docker ps
to see the running containers.
docker ps
You should see the ubuntu
container listed with a status of Up
.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[container_id] ubuntu "sleep 10" About a minute ago Up 5 seconds ...
After about 10 seconds, the sleep 10
command will finish, and the container will stop. Use docker ps -a
again to see all containers, including the stopped one.
docker ps -a
The ubuntu
container should now have a status of Exited
.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[container_id] ubuntu "sleep 10" About a minute ago Exited (0) 5 seconds ago ...
...
Finally, let's stop the ubuntu
container manually using the docker stop
command. You can stop a running container by its ID or name. We will use the container ID that was output when we ran the container. Replace [container_id]
with the actual ID of your ubuntu container.
docker stop [container_id]
This command will output the container ID that was stopped.
[container_id]
Use docker ps
again to confirm that the container is no longer running.
docker ps
The ubuntu
container should not appear in the list of running containers.