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.
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.
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:
To see which containers are currently running, use the docker ps command. This will show a table with important information about each running container:
The output will look something like this:
Here, you can see the container ID, the image it is running, the command used to start it, when it was created, its current status, the ports it is using, and its name. The PORTS column shows that port 8080 on your computer is mapped to port 80 in the container.
If you want to see all containers, including those that have stopped, you can add the -a flag:
This is useful for checking the status of containers you have run in the past.
Sometimes, you need to see what is happening inside your container, especially if something is not working as expected. The docker logs command lets you view the output from a container. For example, to see the logs for your nginx container, run:
You might see output like this:
Logs are helpful for troubleshooting startup issues, checking if your application is running, or seeing requests and errors. If you want to watch the logs in real time as they are written, you can add the -f flag (for "follow"), like this: docker logs -f my-nginx. This is similar to using tail -f on a log file.
Sometimes, you need to look inside a running container to debug, check files, or run commands. The docker exec command lets you do this. To open a shell inside your nginx container, use:
The -it flags make the shell interactive, so you can type commands. Once inside, you can explore the container's file system. For example, you can list the default nginx web page files:
You might see:
When you are done, type exit to leave the container shell and return to your terminal. This ability to run commands inside a container is very useful for debugging, checking configuration files, or viewing logs that are not sent to the main container output.
Note: We use sh instead of bash because sh is available in almost all Linux containers, including minimal variants like Alpine-based images. While some images include bash, many do not to keep the image size small. Using sh is a safer, more portable choice that ensures your commands work across different image types.
Managing containers means being able to stop, start, and remove them as needed. To stop your running nginx container, use:
This will gracefully shut down the container. If you want to start it again, use:
You can repeat this process as many times as you need. When you are finished with a container and want to remove it, you must stop it first. Then, use:
This deletes the container, freeing up resources. Remember, removing a container does not delete the image. You can always create a new container from the same image later.
In this lesson, you learned how to work with existing images from Docker Hub. You practiced pulling an image, running a container in detached mode with a name and port mapping, listing and inspecting running containers, viewing logs, executing commands inside a container, and managing the container's lifecycle by stopping, starting, and removing it. This flow — pull, run, inspect, troubleshoot, and clean up — is the foundation of working with Docker containers.
In the next part of this unit, you will get hands-on practice with these commands. You will repeat these steps, answer short questions, and build confidence in managing containers. Remember, while Docker is already installed in your CodeSignal environment, it is important to know these steps for your own computer as well. You are now ready to start working with real applications in containers.
