Creating your first Docker container is a significant step toward understanding containerization and how it can streamline your development workflow. This blog will walk you through the process step-by-step, from installation to running your first container.
Prerequisites
Before you begin, ensure you have the following:
- Docker Installed: You need Docker installed on your machine. If you haven’t done this yet, refer to the official Docker installation guide for your operating system:
- Install Docker.
- Basic Command Line Knowledge: Familiarity with command line interfaces (CLI) will help you execute the necessary commands.
Step 1: Verify Docker Installation
After installing Docker, verify that it’s installed correctly by running the following command in your terminal or command prompt:
docker –version
You should see output indicating the version of Docker installed, confirming that it’s set up properly.
Step 2: Pull a Docker Image
Docker containers are created from images. For your first container, you can use a simple image like Ubuntu. To pull the Ubuntu image, run:
docker pull ubuntu
This command downloads the latest Ubuntu image from Docker Hub, the official repository for Docker images. You can choose any other image as well, depending on your needs.
Step 3: Create and Run Your First Container
Now that you have the Ubuntu image, you can create and run your first container with the following command:
docker run -it ubuntu
- The run command creates a new container from the specified image.
- The -it flags allow you to interact with the container’s shell.
After running this command, you should be inside the Ubuntu container, with a command prompt that looks like this:
root@<container-id>:/#
Step 4: Explore the Container
While inside the container, you can run any commands you would normally run in a Linux terminal. For example, you can update the package list by executing:
apt-get update
You can also install additional packages, such as curl:
apt-get install curl
Step 5: Exit the Container
To exit the container and return to your host terminal, simply type exit:
exit
This command stops the container and returns you to your original terminal.
Step 6: List Your Containers
To see the containers you’ve created, use the following command:
docker ps -a
This command lists all containers, including those that are stopped. You’ll see the container ID, image, command, and status of each container.
Step 7: Restart a Stopped Container
If you want to restart the container you just exited, use the following command, replacing with the actual ID of your container:
docker start -i <container-id>
The -i flag attaches your terminal to the container’s input, allowing you to interact with it again.
Step 8: Remove a Container
To remove a container when you’re done with it, use:
docker rm <container-id>
If the container is running, you’ll need to stop it first using:
docker stop <container-id>
Conclusion
Congratulations! You’ve successfully created and run your first Docker container. This knowledge will enhance your development workflow and help you create reproducible environments easily.
To explore more about Docker, visit our blog on What is Docker? and if you need assistance or want to learn more, feel free to reach out or request a free consultation with our team!