Run, Stop and Remove Docker Containers

Mark Shust
InstructorMark Shust

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 5 months ago

In this lesson, we'll find out the basics of running Docker containers. We'll go over how to download images from Docker Hub, what happens when you stop containers, how to restart a container once it's been stopped, and also how to remove containers.

[00:00] There are two ways to run Docker containers. Type a simple Docker run command, followed by the name of the image you want to run, in this case, mongo.

[00:09] The container will download from Docker hub if it does not currently exist on your machine. If it does exist, it will pull the image right from your downloaded image cache. The Docker run command will run the container in the foreground and all logs will be outputted to the terminal.

[00:25] Type control-C to stop the container, another way to run the containers is to specify the -d flag, which runs the container as a background daemon. We can check to see which containers are currently running by typing docker ps. To stop a container, type docker stop, followed by the container ID. Let's verify there are no containers running.

[00:47] Just because we stopped a container, doesn't mean it no longer exists. It will remain at a stop or exited state. We can see all containers regardless of status by typing docker ps -a. We can start a container again by typing docker start and then the container ID.

[01:04] Note that you don't need to use the full ID, but just the first few letters, as long as it has uniqueness among the other downloaded image IDs. Let's check to see that the container is indeed running again.

[01:17] To remove a container, type docker rm and the container ID. Note that containers need to be stopped in order for them to be able to be removed. Let's go ahead and stop and remove this running container. Then we can verify all containers have been removed.