Using Docker
What is Docker?
Docker is an open-source containerization platform that allows you to create, deploy, and run applications in containers. Containers allow you to package an application with all of its dependencies and ship it as a single unit, making it easy to deploy and run the application in any environment.
Prerequisites
To use Docker, you will need to install Docker on your machine. You can download the Docker installer from the Docker website (https://www.docker.com/).
You will also need a text editor to write your Dockerfiles and a terminal to run the commands.
Getting Started
To get started with Docker, you will need to create a Dockerfile and a docker-compose.yml file.
A Dockerfile is a text file that contains the instructions for building a Docker image.
A docker-compose.yml file is a YAML file that defines the services that make up your application and how they should be built, deployed, and scaled.
Writing a Dockerfile
To write a Dockerfile, you will need to specify the base image that you want to use for your application. You can use an official image from the Docker Hub (https://hub.docker.com/) or you can create your own base image.
Here is an example of a Dockerfile that uses the official Node.js image as the base image:
FROM node:12-alpine
WORKDIR /app
COPY package.json .
COPY package-lock.json .
RUN npm install
COPY . .
CMD ["npm", "start"]
This Dockerfile specifies the following instructions:
FROM
: Specifies the base image that the Docker image will be built on top of. In this case, the base image is the official Node.js image with version 12 and the Alpine Linux distribution.WORKDIR
: Sets the working directory for the application.COPY
: Copies the specified files from the host machine to the Docker image.RUN
: Runs the specified command in the Docker image. In this case, it runsnpm install
to install the dependencies for the application.CMD
: Specifies the command that will be run when the Docker container is started. In this case, it runsnpm start
to start the application.
Building a Docker Image
To build a Docker image from a Dockerfile, you will need to use the docker build
command.
Here is the syntax for the docker build
command:
docker build [OPTIONS] PATH
For example, to build a Docker image from the Dockerfile in the current directory, you can use the following command:
docker build .
This command will build a Docker image using the instructions in the Dockerfile and give it a unique ID.
Running a Docker Container
To run a Docker container from a Docker image, you will need to use the docker run
command.
Here is the syntax for the docker run
command:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
For example, to run a Docker container from the Docker image that you built in the previous step,
you can use the following command:
docker run IMAGE
Replace IMAGE
with the ID or name of the Docker image that you want to run.
By default, the docker run
command will create a new container from the image and start the application. You can also use the -d
flag to run the container in detached mode, which means that the container will run in the background and the command prompt will return immediately.
For example:
docker run -d IMAGE
Viewing Running Containers
To view the containers that are currently running on your machine, you can use the docker ps
command.
Here is the syntax for the docker ps
command:
docker ps [OPTIONS]
For example, to view all running containers, you can use the following command:
docker ps
To view all containers (running and stopped), you can use the -a
flag:
docker ps -a
Stopping a Container
To stop a running Docker container, you can use the docker stop
command.
Here is the syntax for the docker stop
command:
docker stop CONTAINER
Replace CONTAINER
with the ID or name of the container that you want to stop.
For example:
docker stop CONTAINER
Removing a Container
To remove a Docker container, you can use the docker rm
command.
Here is the syntax for the docker rm
command:
docker rm CONTAINER
Replace CONTAINER
with the ID or name of the container that you want to remove.
For example:
docker rm CONTAINER
Using docker-compose
In addition to using the docker
commands to build and run containers, you can also use docker-compose
to simplify the process.
docker-compose
is a tool that allows you to define the services that make up your application in a docker-compose.yml
file and then run them with a single command.
Here is an example docker-compose.yml
file for a simple Node.js application:
version: "3"
services:
app:
build: .
command: npm start
volumes:
- .:/app
ports:
- "3000:3000"
This file defines a single service, app
, which is built from the Dockerfile
in the current directory and runs the npm start
command. It also mounts the current directory as a volume in the container and exposes port 3000.
To build and run the services defined in a docker-compose.yml
file, you can use the docker-compose up
command.
Here is the syntax for the docker-compose up
command:
docker-compose up [OPTIONS] [SERVICE...]
For example, to build and run the app
service defined in the docker-compose.yml
file above, you can use the following command:
docker-compose up
This command will build the app
service and run it in a container.
To stop the services and remove the containers, you can use the docker-compose down
command.
Here is the syntax for the docker-compose down
command:
docker-compose down [OPTIONS] [SERVICE...]
For example:
docker-compose down
This command will stop the app
service and remove the container.
Additional Resources
For more information on using Docker, you can refer to the Docker documentation (https://docs.docker.com/).ddocker
Last updated