Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Docker Commands for Development to Deployment

DZone's Guide to

Docker Commands for Development to Deployment

This article lists and explains all the commands needed for a Docker container, from developing the image to deployment.

Free Resource

“Automated Testing: The Glue That Holds DevOps Together” to learn about the key role automated testing plays in a DevOps workflow, brought to you in partnership with Sauce Labs.

The objective of this article to understand the end to end flow of container development to deployment in the target environment and list the Docker commands needed for every action.

1. Introduction

The overall process consists of developing a container image with your code, dependent software, and configurations, running and testing the container in a development environment, publishing the container image into Docker Hub, and finally, deploying the Docker image and running the container in the target environment. This article assumes that you have installed Docker engine in the development and target environment. Please refer to 6.3 for installation instructions.

2. Develop Container Image

To build the container image, we have to create a dockerfile which will contain all the necessary information. Please refer here to develop the dockerfile.

2.1 Build Docker Container

$docker build -t containername .

This command will take the Dockerfile present in your current directory. If you have the dockerfile in a different name and different location, we can use -f flag to specify the docker file name. The “docker build” command will build the container image in the name specified with “-t” flag.

$docker build -t myapp .

01-DockerBuild

2.2 Docker Image Naming Convention

You can provide any name to a Docker container, when you run locally.  It could be as simple as “myApp” as shown above. But if you want to publish the image into Docker Hub, there is a specific naming convention to be followed. This convention helps the Docker tools to publish the container image into right namespace and repository.

The format:

NameSpace/Repository:Version

So, I build the docker image using the above convention:

$docker build -t saravasu/techietweak:001 .

We can also use the “docker tag” command to create an image from an existing image. The “docker tag” command is explained below.

2.3 List All Images in the Docker

$docker images

02-DockerImagesList

3. Run the Container

3.1 Start the Docker Container

Use the “docker run” command to start the Docker container.

$docker run -d -p 8080:8080 saravasu/techietweak:001

03-DockerRun

The “-d” option runs the container in the detached mode, so that the container continues to run, even if the terminal is closed.

The “-p” command used to map the ports. In this example, “-p 8080:8080” the first port number is the port used the Docker host. The second port number is used by the Docker container. As per this command, all the traffic comes to the Docker host port, will be forwarded to the Docker container port. 

3.2 Check Current Running Containers

$docker ps

07-DockerPS

From the above output, we can see that a Docker container is running in the name of “trusting_snyder.”

To list all the containers irrespective of the states, use the “-a” switch.

$docker ps -a

3.3 Show the Console Logs of the Running Container

$docker logs <containerName>

08-dockerlogs

ContainerName can be found with the “docker ps” command.

3.4 Log into the Container

$docker exec -it containerId /bin/bash

The above command will prompt you with “bash” shell of the container.

09-dockerlogin

3.5  Stop the Running Container

$docker stop <containername>

11-DockerStop

3.6  Remove the Container Image From Docker

$docker rm imageId

Find the imageId of the container using the command “docker images” or “docker images -a.”

$docker rmi -f <List Of Image Ids>

11-DockerRMIImages

The above command will forcefully delete the given image.

3.7 Clean Up Your Docker/Delete All Container Images in the Local Docker

$docker rmi -f $(docker images | tr -s ' ' ' ' | cut -d' ' -f3)

4. Publish the Container Image

The Docker container images can be published to your local dockyard or the public Docker hub. The process and commands are the same for both. To publish your Docker image in the Docker Hub, first create your namespace and repository at http://hub.docker.com.

I have used my namespace “saravasu” and the repository “techietweak” for this exercise.

00-dockerhubRepo

4.1 Log Into Docker Hub

$docker login

If you want to log into your local repository, please provide the URL. If a URL is not specified, then this command will log into Docker Hub.

$docker login http://localhost:8080

04-DockerLogin

4.2 Tag the Container Image

To push the Docker container image into Docker Hub, it must be tagged in a specific format:  <Namespace>/<Repository>:<Version>. If you don’t specify the version, it will be taken as “default.” In the below command, I am tagging the image:

$docker tag myapp:latest saravasu/techietweak:001

4.3 Push the Docker Image Into Docker Hub

$docker push saravasu/techietweak:001

10-DockerPushToDockerHub

4.4 Check the Container Images in Docker Hub

Now log into your Docker Hub account and check for the images in the respective repository.

05-dockerhubimages

5 Deploy the Container

5.1 Pull the Docker Container Image

Login into Docker Hub from the host machine in the target environment and pull the container image from Docker Hub. If you want to pull it from your private dockyard, use the command “$docker login <hostname>” to specify the hostname of the private dockyard.

$docker login

The above command will log into https://hub.docker.com, since the host name is not specified.

$docker pull saravasu/techietweak:001

12-dockerPull

5.2 Check the Image

The docker pull command downloaded the container image from the Docker Hub. We can validate the same by using the “docker images” command.

 $docker images

06-DockerPull-Images

5.3 Run the Container

Now we can run the Docker container in the same way we ran it in the development environment and test it in the same way we have done before.

$docker run -d -p 8080:8080 saravasu/techietweak:001

13-dockerTargetRun

The docker run command starts the container.  To validate it, we can use the “docker ps” command. Docker has created a new container and it is running in the name of “naughty_lewin.”

As we see above, the Docker engine provides a random name to the running container, but this could be a problem in automation, so it is always good to specify a name we want to refer. This can be achieved by using the “–name” parameter.

$docker run -d -p 8080:8080 --name "myNodeJsWebContainer" saravasu/techietweak:001

14-dockerRunNamedContainer

6. Summary

This article captures the flow and necessary commands to develop a container image, run it in the local environment, publish the image to Docker Hub, and run the container in the target environment. Further study and detailed documentation are available on the Docker website [refer to 6.1].

Learn about the importance of automated testing as part of a healthy DevOps practice, brought to you in partnership with Sauce Labs.

Topics:
devops ,docker ,containers

Published at DZone with permission of Saravanan Subramanian, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}