jueves, 12 de enero de 2023

Docker command coobook

1 Create a container from  an image  ( if image is not locally present it will download) with bash access

docker run -it <image_id> /bin/bash

2 Show only the  running container

docker ps


3 Show  all container

docker ps -a


4 Show all images on the system

docker images


5 Download an image only

docker pull image-name


6 Start  a container with bash access

docker run -it e98b6ec72f51 /bin/bash


Run a docker container on background on detach mode


docker container run -d e98b6ec72f51


Start a container interactive and public the ports

docker run -ti -p 7070:80  0b8c43b4b6a4

stop a container

 docker stop  284631a6df50 



7 Restart  a  docker container

and the -i flag to make it interactive, followed by either the container ID or name. Be sure to substitute the ID of your container in the command below:

  1. docker start -ai 11cc47339ee1


7 Deleting  a container

  1. docker rm 11cc47339ee1

How do I SSH into a running container

There is a docker exec command that can be used to connect to a container that is already running.

  • Use docker ps to get the name of the existing container
  • Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container
  • Generically, use docker exec -it <container name> <command> to execute whatever command you specify in the container.


8 Remove  a docker all docker container with  exited status

 docker rm $(docker ps --filter status=exited -q)

Copying files to and from container


1. Container -> Local Host

sudo docker cp container-id:/path/filename.txt ~/Desktop/filename.txt


 Local Host -> Container


# Copy db.config from the current working directoty to a container da3430062137 /opt/app/db.config

$ docker cp db.config d362659da5fc:/opt/app/


To save an existing container as an image, save that image as a tar file, and create a new container from that image, you can follow these steps:

  1. Save the container as an image:

    php
    docker commit <container_id> <image_name>

    Replace <container_id> with the ID or name of the container you want to save, and <image_name> with the desired name for the image.

  2. Verify that the image has been created:

    docker images
  3. Save the image as a tar file:

    php
    docker save -o <output_file.tar> <image_name>

    Replace <output_file.tar> with the desired name for the tar file and <image_name> with the name of the image you want to save.

  4. Verify that the tar file has been created.

  5. To create a new container from the saved image, first, load the image from the tar file:

    css
    docker load -i <input_file.tar>

    Replace <input_file.tar> with the name of the tar file containing the image.

  6. Verify that the image has been loaded:

    docker images
  7. Create a new container from the loaded image:

    php
    docker run --name <new_container_name> <image_name>

    Replace <new_container_name> with the desired name for the new container and <image_name> with the name of the loaded image.

  8. Verify that the new container has been created and is running:

    css
    docker ps -a

These steps should allow you to save an existing container as an image, save that image as a tar file, and create a new container from that saved image.


https://www.digitalocean.com/community/tutorials/working-with-docker-containers

https://phoenixnap.com/kb/docker-run-command-with-examples


 https://linuxhint.com/how-to-create-a-docker-image/

https://cloudinfrastructureservices.co.uk/how-to-setup-freepbx-using-docker-build-freepbx-docker-container/

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-22-04

https://phoenixnap.com/kb/docker-run-command-with-examples

https://www.docker.com/blog/how-to-use-the-alpine-docker-official-image/


No hay comentarios:

Publicar un comentario