Docker Commands
Docker Commands
docker version
- shows docker versiondocker run name_of_the_image
- starts a containerdocker run name_of_the_image:name_of_the_tag
- starts specific version of a containerdocker run name_of_the_container sleep number_of_seconds
- container sleeps after n seconddocker ps
- shows a list of running containersdocker ps -a
- shows list of all containers (stopped & running)docker stop name_of_the_container
- stops a containerdocker rm name_of_the_container
- removes a containerdocker images
- shows list of imagesdocker rmi name_of_the_image
- removes an imagedocker pull name_of_the_image
- downloads an imagedocker exec name_of_the_container cat /etc/hosts
- executes a commanddocker attach name_or_id_of_the_container
- runs a container in attach modedocker run -d name_of_the_container
- runs a container in detach modedocker run -it name_of_the_container
- runs a container withi
nteractive andt
erminal modedocker run -p port_number_of_local_host : docker_container_port name_of_the_container
- mapsport_number_of_local_host
todocker_container_port
docker run -v directory/outside/the/container : directory/inside/the/container name_of_the_container
- mounts thedirectory/outside/the/container
todirectory/inside/the/container
docker inspect name_of_the_container
- returns details in a JSON formatdocker logs name_or_id_of_the_container
- returns logs of the container
Docker Images
Before creating the docker image we have to create a docker file call it Dockerfile. After creating the docker file we will create an image with the docker command.
Dockerfile
Docker file is a text file that consists of two parts instructions and arguments.
FROM Ubuntu // Start from a base operating system or another image
RUN agt-get update // Install all dependencies
RUN agt-get install python // Install all dependencies
RUN pip install flask // Install all dependencies
RUN pip install flask-mysql // Install all dependencies
COPY . /opt/source-code // Copy source code
ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run // Specify Entrypoint
Docker file works in layers. If any layer fails then the docker will reuse the previous layers from the cache and start from when it failed.
Creating & Pushing Docker Image Command
To create an image locally we will use the following command:
docker build Dockerfile -t name/imageName
And to push the image to Docker Registry, we will use the following command:
docker push accountName/imageName