Getting Started with Docker
Now that Docker is up and running, let's explore the fundamental actions you can perform with it.
Pulling Images
Docker boasts a vast community with numerous preconfigured images, many maintained by official software developers (e.g., MySQL, Nginx, Jenkins, and more). These images are accessible on Docker Hub, and while you don't need an account to pull images, you'll require one if you plan to host your own.
For official images:
docker pull <repository>
For user-specific images:
docker pull <username>/<repository>
To pull a specific version:
docker pull <username>/<repository>:<tag>
Example:
docker pull busybox:1.25.0
To view your images, use:
docker images
Creating Images from Dockerfiles
Crafting Docker images involves creating Dockerfiles, detailing the steps to execute based on an existing image.
Creating Containers
When you use an image, you instantiate it, creating a container. Multiple containers can arise from the same image, akin to classes and objects in Object-Oriented Programming.
To create a container:
docker run busybox
This seemingly uneventful command conceals Docker's magic: it creates, executes, and then concludes the container, as no specific actions were specified.
Execute a command inside the container:
docker run busybox echo "Hello world!"
Enable an interactive shell:
docker run -it busybox
For a more comprehensive example, let's pull the Nginx image:
docker pull nginx
These basic steps lay the foundation for working with Docker—pulling images, creating custom ones, and generating containers for your applications.
Running Nginx in a Docker Container
Now, let's put our pulled Nginx image to use by running it with the following command:
docker run -p 8080:80 -d nginx