docker run :
Docker run command is use to create a container from an image. when we run docker run < image-name >, docker-api sends this command to docker daemon which will create an instance of image (container) application into the docker host.
docker run is equivalent to the API /containers/create then /containers/(id)/start
First It will search the image from docker host system if it found then it will create a container otherwise it will go out to docker hub and pull the image. From second time onwards if we run the same image docker will find that in docker host system and won't go out to docker hub.
- Options with Run command.
-d (deteched mode) : it will run the conatainer on background.
attach < container_id/name: it will attach the background running job to the foreground.
# Syntax :
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
# Examples :
$ docker run python:3.11.0a7
Run - Port Mapping : Publish or expose port
When we want to run multiple instance of a service or application we can use docker run with port(-p) option. which will create container on different ports of docker host sytem and mapped to container ip.
By using port mapping we can access the application outside of the host machine -- publically.
How to access application outside of the host machine.
# Port Mapping (-p, --expose):
$ docker run -p [docker-host-port]:[container-port] image
$ docker run -p 80:8080 mysql
$ docker run -p 81:8080 mysql
$ docker run -p 8306:8080 mysql
data persistent in docker
When we stop or remove a container all the data inside it will be deleted.
If we want to keep the data even if the container is not active/alive then we need to mapped a directory outside of the conatainer and inside of docker host.
Create a directory inside of docker-host and mount the container volume
# Volume Mapping (--volume, -v):
$ docker run -v [docker-host/dir]:[container/data/path] image-name
$ docker run -v /opt/datadir:/var/lib/mysql mysql
Get the details of a Container
# inspect the Container:
$ docker inspect container-name
Container logs :
# View the Container Logs :
$ docker logs container-name
docker run commnad with some useful options and arguments:
Name | Syntax | Description |
---|---|---|
Detached (-d) | docker run -d IMAGE | Run container in background. |
--interactive, -i | $ docker run -i IMAGE | Keep STDIN open even if not attached. |
-it | docker run -it IMAGE | attach with Terminal with interactive mode. |
bash | docker run -it IMAGE bash | Open a command line tool and attach Terminal with interactive mode. |
--expose, -p | docker run -p [docker-host-port]:[container-port] image | To publish the application/service globally. |
--volume, -v | docker run -v [dock-host/dir]:[cont./data/path] image-name | To persistent the data in docker. |
docker build commnad with some useful options and arguments:
Name | Syntax | Description |
---|---|---|
-t | docker build . -t IMAGE Tag_Image_Name | It will tag the image to Tag_Image_Name. |