To run a Docker container from a Docker image, you need to use the docker run command. The docker run command starts a new Docker container based on the specified image. Here are the steps to run a Docker container from a Docker image:
Open a terminal window and run the docker run command with the following syntax:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
The IMAGE argument specifies the name and tag of the Docker image to use for the container. The OPTIONS and COMMAND arguments are optional and can be used to customize the behavior of the container.
For example, to run a Docker container based on the nginx image and expose port 80 on the host system, you would run the following command:
docker run -p 80:80 nginx
Wait for the Docker container to start. This may take a few seconds, depending on the size of the image and the resources available on your system.
Verify that the Docker container is running by running the docker ps command. This command lists all of the Docker containers that are currently running on your system.
docker ps
This should display a list of Docker containers that includes the new container you just started, along with its container ID, image name, and other information.
Here are some additional options that you can use with the docker run command:
-d: This option runs the container in detached mode, which means that it runs in the background and does not attach to the terminal.
-e: This option allows you to set environment variables for the container. These variables can be used to configure the behavior of the container or the application running inside it.
–name: This option allows you to specify a custom name for the container, which can make it easier to identify and manage the container.
-v: This option allows you to mount a volume into the container, which can be used to share files and data between the container and the host system.
For example, to run a Docker container based on a custom image named my-node-app in detached mode, with a custom name of my-app-container, and with a volume mounted to a local directory, you would run the following command:
docker run -d --name my-app-container -v /path/to/local/directory:/app my-node-app
This command starts a new Docker container based on the my-node-app image, runs the container in detached mode, sets the custom name of my-app-container, and mounts the /path/to/local/directory directory on the host system to the /app directory inside the container.