Building Your First Container with a HelloWorld Image using Docker CLI
1. What is Docker CLI?
Docker is an open-source platform, and it is used to containerize the application. It packages the application along with the dependencies into the containers, which is easier to deploy.
Docker CLI is a command-line interface for Docker that helps to build images and start and stop the containers from the command line.
2. Docker CLI commands help
a. Open the terminal or PowerShell.
b. Run the command ‘docker –help’ to get help on Docker CLI commands.
c. Run the command ‘docker network –help’ to get the command-specific help.
d. Run the command ‘docker network create –help’ to know the options for the specific command method.
3. Create a docker container from CLI (Method 1):
3.1. Create a Docker container from ‘Hello world’ image
a. Run the command ‘docker container create imagename’ to create a docker container from the ‘Hello world’ image.
b. Example: docker container create hello-world:latest
c. It will check for the hello-world image locally and start the container if it is found; otherwise, it downloads from the Docker Hub.
3.2. List the Docker Containers
a. Run the command ‘docker ps’ to list the active containers. The following images don’t show our container because it will list only the active containers.
b. Run the command ‘docker ps –list’ to list all the containers, both active and inactive.
3.3. Start the container
a. Run the command ‘docker start container ‘ to start the containers.
- Example: Run the command ‘docker start container 25c8937cabb1’ to start our container.
b. Run the command ‘docker ps -all’ to list all the containers again.
c. You can see the container status, which created and exited about a minute ago message in the screenshot.
3.4. Docker Logs
a. Run the command ‘docker logs ‘ to view the logs of the container.
b. Example: Run the command ‘docker logs 25c8937cabb1’ to view our container logs.
c. You can also attach the logs while starting the container.
d. Run the command ‘docker container start –attach containerid’ to attach the logs while starting the container.
e. Example: Run the command ‘docker container start –attach 25c8937cabb1’ to attach the logs while starting our container.
f. We can start the same container multiple times because the containers won’t be deleted and can be started anytime.
4. Create a docker container from CLI (Method 2):
4.1. Create a docker container from ‘Hello world’ image
a. Run the command ‘docker run ImageName’ to create a container from the given image.
b. Example: Run the command ‘docker run hello-world:latest’ to create a container and run the image for the ‘Hello world’ latest image.
4.2 Docker run
a. Basically, the following actions will happen with docker run: docker run = docker container create + start + attach
b. Run the command ‘docker ps –all’ to list all the docker containers to view the newly created container.
c. Run the command ‘docker logs ‘ to view the Docker container logs.
d. Run the command ‘docker logs 3380c26f2f2f’ to view the newly created Docker container logs.
Leave a comment