Managing Docker Containers
Introduction And Goals
Welcome back to Docker Fundamentals. In the previous lesson, you learned what containers are, how they differ from virtual machines, and why they are so useful for developers. You also ran your very first container and interacted with Docker using Python code. In this lesson, you will take the next step and learn how to work with real-world images from Docker Hub. Specifically, you will practice pulling, running, inspecting, executing commands inside, stopping, starting, and removing a container using the popular nginx web server image.
By the end of this lesson, you will be able to manage the full lifecycle of a container using basic Docker commands. These are the essential skills you will use every day when working with containers, whether you are developing, testing, or deploying applications. Let's get started.
Pulling Images From Docker Hub
Before you can run a container, you need an image. Docker images are like blueprints for containers. Most images are stored on Docker Hub, which is a public registry where you can find thousands of ready-to-use images for popular software.
To download the latest version of the nginx image, use the docker pull command. Here is how you do it:
This command tells Docker to download the nginx image with the latest tag from Docker Hub. The latest tag is the default and usually points to the most recent stable version. You can also pull a specific version by changing the tag, for example, nginx:1.25.4. Using specific tags is important when you want to make sure your application always uses the same version and does not break if the latest version changes.
If you try to run a container with an image you do not have yet, Docker will automatically pull it for you. However, it is a good habit to pull images yourself when you want to control exactly what is downloaded and when.
When you run the pull command, you will see output like this:
This confirms that the image is now available on your system and ready to use.
Running A Container (Detached, Named, And Port-Mapped)
Now that you have the nginx image, you can start a container from it. In this example, you will run the container in the background (detached mode), give it a name, and map a port from your computer to the container. Here is the command:
Let's break down what each part does:
-d: Runs the container in detached mode, which means it runs in the background and does not block your terminal.--name my-nginx: Gives your container a friendly name, making it easier to manage later.-p 8080:80: Maps port8080on your computer (the host) to port80inside the container, which is wherenginxserves web pages by default. This means you can access thenginxwelcome page by visitinglocalhost:8080in your browser.nginx:latest: Specifies the image to use for creating the container.
Naming containers is helpful because you can refer to them by name instead of using the automatically generated container ID. Detached mode is useful when you want your container to keep running while you do other things in your terminal. Port mapping is essential for making services inside your container accessible from your computer.
After running the command, you will see a long string of characters, which is the container ID:
This means your container is up and running in the background.
