KAHIBARO Bookshelves
Discord Login Register

4.1. Running Your First Container

Getting Ready to Run a Container

To run your first container, you only need a working Docker installation and access to a terminal or command prompt. At this stage, you do not need to understand images or Dockerfiles in depth, only that a container is created from an image that already exists somewhere, such as Docker Hub.

Make sure Docker is installed and the Docker daemon is running. On many systems, you can confirm this by running a simple command like docker version or docker info. If Docker responds with version and system details, you are ready to start your first container.

You must have the Docker service running. If docker commands fail with connection errors, start Docker before trying to run containers.

Running a Simple Container

The most common way to start a container is with the docker run command. Your first container should use a very simple image so you can see the result clearly without extra setup.

One popular choice for a first command is:

docker run hello-world

When you execute this command, Docker looks for the hello-world image locally. If the image is not present, Docker contacts a registry, usually Docker Hub, pulls the image to your machine, and then starts a container from it. The hello-world image prints a friendly message explaining what Docker just did, then the container exits.

This small example proves several things at once. It shows that Docker can reach the registry, that the image can be downloaded, and that your system can successfully start and run a container.

If docker run hello-world prints the explanation message and returns you to the prompt, your Docker installation and basic networking are functioning correctly.

Seeing the Effect of a Container That Exits

A container created from hello-world runs only long enough to print its message. By the time you see the output, the container has already stopped. This is normal for containers that perform a single short task.

The important lesson is that starting a container does not guarantee that it will keep running. Containers stay alive only while their main process is running. If that main process finishes or fails, the container stops. For simple demonstration images, this means you often run the container, see the output, and then the container exits immediately.

You will learn how to list stopped containers and remove them in later chapters. For now, it is enough to observe that closing the main process ends the container’s life.

Running a Long-Lived Container

To see a container that keeps running, you can start something that provides a service, such as a web server, instead of a short one-off command.

An example is:

docker run nginx

This command uses the nginx image, which starts an Nginx web server as its main process. After you run this command, the container continues running as long as the Nginx process is alive. Because Nginx is a server process, it stays active rather than exiting immediately.

In many terminals, when you run a server image like this without extra options, you will see log output appear directly in your terminal, and the prompt will not return until you stop the container. This shows that you are now attached to a running container whose main process is still active.

A container keeps running only while its main process is running. When that process stops, the container stops.

Understanding Default Behavior on First Run

When you run a container without many options, Docker makes several default choices for you. It uses the image’s own default startup command, it attaches your terminal to the container’s main process, and it uses some basic networking and storage settings in the background.

For a first container, relying on these defaults is useful. You can start a container with a short single command and immediately see output or behavior without needing to specify all details. Later chapters will show you how to customize what command runs inside the container, how to manage ports, and how to control interaction.

For now, focus on these basic patterns:

Use docker run hello-world when you want a quick verification that Docker can pull and run images.

Use a simple server image like docker run nginx or a similar image if you want to see a container that stays alive, produces continuous logs, and demonstrates a long-running process.

Confirming That You Actually Used a Container

After running your first container, it is useful to convince yourself that this was not just a local script or program running normally on your machine. The output from hello-world helps with this, because it explicitly describes how Docker pulled the image and started a container.

As you experiment, try running docker run hello-world again. The second time, Docker will not need to download the image, because it is already stored locally. The output will still show the container message, but you will notice that the image pull step is skipped and the container starts faster. This behavior shows that the image and the container are distinct. The image remains available, and each docker run uses that image to create a new, separate container instance.

This repeated pattern, an image stored once, and many containers created from it, is at the heart of working with Docker. Your first few runs are a simple but real version of this pattern.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Introduction
    • ☰ 1.1. What is Docker?
      • ☰ 1.2. Why Docker?
        • ☰ 1.3. Virtual Machines vs Containers
          • ☰ 1.4. When Not to Use Docker
            • ☰ 1.5. How Docker Is Used in Real Projects
            • ☰ 2. Core Concepts
              • ☰ 2.1. Images
                • ☰ 2.2. Containers
                  • ☰ 2.3. Docker Engine and Architecture
                    • ☰ 2.4. Docker Hub and Container Registries
                      • ☰ 2.5. Tags, Versions, and Digests
                      • ☰ 3. Installing Docker
                        • ☰ 3.1. Docker on Linux
                          • ☰ 3.2. Docker on Windows
                            • ☰ 3.3. Docker on macOS
                              • ☰ 3.4. Verifying the Installation
                                • ☰ 3.5. Common Installation Issues
                                • ☰ 4. Your First Docker Container
                                  • ☰ 4.1. Running Your First Container
                                    • ☰ 4.2. Understanding `docker run`
                                      • ☰ 4.3. Interactive vs Detached Mode
                                        • ☰ 4.4. Stopping and Removing Containers
                                          • ☰ 4.5. Listing Containers
                                          • ☰ 5. Working with Docker Images
                                            • ☰ 5.1. Pulling Images from Docker Hub
                                              • ☰ 5.2. Listing and Removing Images
                                                • ☰ 5.3. Image Layers Explained
                                                  • ☰ 5.4. Image Size Optimization
                                                    • ☰ 5.5. Inspecting Images
                                                    • ☰ 6. Dockerfiles – Building Your Own Images
                                                      • ☰ 6.1. What Is a Dockerfile?
                                                        • ☰ 6.2. Basic Dockerfile Structure
                                                          • ☰ 6.3. Common Instructions (`FROM`, `RUN`, `COPY`, `CMD`, `ENTRYPOINT`)
                                                            • ☰ 6.4. Building Images with `docker build`
                                                              • ☰ 6.5. Best Practices for Dockerfiles
                                                                • ☰ 6.6. Multi-Stage Builds
                                                                • ☰ 7. Managing Data with Volumes
                                                                  • ☰ 7.1. Why Containers Are Ephemeral
                                                                    • ☰ 7.2. Bind Mounts vs Volumes
                                                                      • ☰ 7.3. Creating and Using Volumes
                                                                        • ☰ 7.4. Inspecting and Removing Volumes
                                                                          • ☰ 7.5. Real-World Examples
                                                                          • ☰ 8. Networking in Docker
                                                                            • ☰ 8.1. Docker Networking Basics
                                                                              • ☰ 8.2. Bridge Networks
                                                                                • ☰ 8.3. Port Mapping
                                                                                  • ☰ 8.4. Container-to-Container Communication
                                                                                    • ☰ 8.5. Custom Networks
                                                                                    • ☰ 9. Docker Compose
                                                                                      • ☰ 9.1. Why Docker Compose?
                                                                                        • ☰ 9.2. `docker-compose.yml` Structure
                                                                                          • ☰ 9.3. Defining Services, Networks, and Volumes
                                                                                            • ☰ 9.4. Running Multi-Container Applications
                                                                                              • ☰ 9.5. Web App and Database Example
                                                                                                • ☰ 9.6. Docker Compose Best Practices
                                                                                                • ☰ 10. Debugging and Monitoring Containers
                                                                                                  • ☰ 10.1. Viewing Logs
                                                                                                    • ☰ 10.2. Executing Commands in Running Containers
                                                                                                      • ☰ 10.3. Inspecting Containers
                                                                                                        • ☰ 10.4. Resource Usage
                                                                                                          • ☰ 10.5. Common Errors and Fixes
                                                                                                          • ☰ 11. Security Basics
                                                                                                            • ☰ 11.1. Running Containers as Non-Root
                                                                                                              • ☰ 11.2. Image Trust and Verification
                                                                                                                • ☰ 11.3. Secrets and Environment Variables
                                                                                                                  • ☰ 11.4. Reducing Attack Surface
                                                                                                                    • ☰ 11.5. Security Best Practices
                                                                                                                    • ☰ 12. Performance and Optimization
                                                                                                                      • ☰ 12.1. Reducing Image Size
                                                                                                                        • ☰ 12.2. Caching and Layer Ordering
                                                                                                                          • ☰ 12.3. Resource Limits
                                                                                                                            • ☰ 12.4. Choosing the Right Base Image
                                                                                                                            • ☰ 13. Docker in Development Workflows
                                                                                                                              • ☰ 13.1. Docker for Local Development
                                                                                                                                • ☰ 13.2. Hot Reloading with Volumes
                                                                                                                                  • ☰ 13.3. Docker and Git
                                                                                                                                    • ☰ 13.4. Docker in CI/CD Pipelines
                                                                                                                                      • ☰ 13.5. Common Development Patterns
                                                                                                                                      • ☰ 14. Deployment Basics
                                                                                                                                        • ☰ 14.1. Running Containers in Production
                                                                                                                                          • ☰ 14.2. Environment Configuration
                                                                                                                                            • ☰ 14.3. Restart Policies
                                                                                                                                              • ☰ 14.4. Logging Strategies
                                                                                                                                                • ☰ 14.5. Basic Scaling Concepts
                                                                                                                                                • ☰ 15. Beyond the Basics
                                                                                                                                                  • ☰ 15.1. Docker Swarm Overview
                                                                                                                                                    • ☰ 15.2. Kubernetes vs Docker
                                                                                                                                                      • ☰ 15.3. Container Registries
                                                                                                                                                        • ☰ 15.4. When to Move Beyond Docker
                                                                                                                                                        • ☰ 16. Practical Projects
                                                                                                                                                          • ☰ 16.1. Dockerizing a Simple Web App
                                                                                                                                                            • ☰ 16.2. Dockerizing a Backend API
                                                                                                                                                              • ☰ 16.3. Full-Stack App with Docker Compose
                                                                                                                                                                • ☰ 16.4. Common Project Pitfalls
                                                                                                                                                                • ☰ 17. Troubleshooting and FAQ
                                                                                                                                                                  • ☰ 17.1. Common Beginner Mistakes
                                                                                                                                                                    • ☰ 17.2. Docker Command Cheat Sheet
                                                                                                                                                                      • ☰ 17.3. FAQ
                                                                                                                                                                        • ☰ 17.4. Useful Resources