Important docker commands to work.

List of docker images
> docker images

List of running docker containers
> docker ps -a

Remove docker images
> docker rmi -f IMAGE_ID

Remove docker container
> docker rm -f CONTAINER_ID

Build docker image
>docker build -t your_dockerhub_username/your_image_name :version_name .

Run docker image
>docker run your_image_name
or
>docker run --name your_container_name -p 8081:8080 -d your_image_name
or
>docker run -it --rm -p 9898:4200 --name your_container_name img_name
It will automatically remove the container when it is stopped.

-p: This publishes the port on the container and maps it to a port on our host. We will use port 80 on the host, but you should feel free to modify this as necessary if you have another process running on that port. For more information about how this works, see this discussion in the Docker docs on port binding.
-d: This runs the container in the background.
–name: This allows us to give the container a memorable name.

Inspect docker container
>docker inspect container_ID

Check files inside docker container
>docker exec -t -i mycontainername /bin/bash

 Check files inside docker image
>docker run -it image_name sh
Then you can check current directory of container or image
>pwd
Also you can list files of container/img
>ls
To exit from that terminal
>exit

For checking container logs if something doesn’t work
>docker logs YOUR_CONTAINER_NAME

To remove everything (container/image)
>docker container rm -f $(docker container ls -qa)
>docker image rm -f $(docker image ls -q)

Tag your image before push
>docker tag IMAGE_ID your_dockerhub_username/IMG_NAME:version

Push image to docker hub
>docker push your_dockerhub_username/IMG_NAME:version

Pull image from docker hub
>docker pull your_dockerhub_username/your_image_name :version_name

Login docker account
>docker login -u your_dockerhub_username

The following builds the images if the images do not exist and starts the containers:
>docker-compose up
If you add the –build option, it is forced to build the images even when not needed:
>docker-compose up --build
If you want to skip the build process
>docker-compose up --no-build
Build the images but doesn’t start the container.
>docker-compose build
If the images aren’t built beforehand, it fails. The --no-cache option disables the Docker build cache in the image creation process. This is used to cache each layer in the Dockerfile and to speed up the image creation reusing layers (~ Dockerfile lines) previously built for other images that are identical.

Run a specific compose file
>docker-compose -f docker-compose-prod.yml up

docker-compose build command will verify changes in Dockerfile and recreate image in case when Dockerfile was changed.
docker-compose up -d command will recreate and restart only container not image.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s