Docker Run Command

Docker Run Command

What is docker run command?

The docker run command creates running containers from images and can run commands inside them.

What is docker command used for?

Docker commands used for create, run, stop, remove, and manage Docker containers easily. These commands can help you automate and streamline the process of deploying and managing your applications in a containerized environment.

How to use docker run commands with example?

  • Run a container using a tag
docker run redis
docker run redis:4.0
  • Run a container using stdin
docker run dagapradesh/example-mapping-docker
docker run -i dagapradesh/example-mapping-docker
docker run -it dagapradesh/example-mapping-docker
  • Run a container using Port Mapping
docker run dagapradesh/webapp
docker run -p 90:5000 dagapradesh/webapp
docker run -p 3306:3306 mysql
  • Run a container using Volume Mapping
docker run -v /opt/datadir:/var/lib/mysql mysql
  • Run a container using a Inspect
docker inspect dagapradesh
  • Run a container to check logs
docker logs dagapradesh

Example

  1. We will take example of Jenkins and will install.
docker run jenkins/jenkins
  1. Internal IP and External IP we can get for web hosting using below command

     docker inspect CONTAINER ID
    
  2. External How to access(Add Port Mapping)

docker run -p 8080:8080 jenkins/jenkins

If you destroy docker containers and run a new container and if you'd like to persist your configuration go to map volume

docker run -p 8080:8080 -v /root/jenkins_data/:/var/jenkins_home -u root jenkins/jenkins
  1. How to use this image?

This will store the workspace in /var/jenkins_home. All Jenkins data lives in there - including plugins and configuration. You will probably want to make that a persistent volume (recommended):

docker run -p 8080:8080 -p 5000:5000 jenkins

This will store the Jenkins data in /your/home on the host. Ensure that /your/home is accessible by the Jenkins user in container (Jenkins user - uid 1000) or use -u some_other_user parameter with docker run.

docker run -p 8080:8080 -p 5000:5000 -v /your/home:/var/jenkins_home jenkins
  1. You can also use a volume container

Then my Jenkins container has the volume (please do read about docker volume handling to find out more).

docker run --name myjenkins -p 8080:8080 -p 50000:50000 -v /var/jenkins_home jenkins

If you liked what you read, do follow and Any feedback for further improvement will be highly appreciated!

Thank you and Happy Learning!👏