How to Get Shell Access to Running Docker Container

#1. Using Docker Attach
You can get bash shell access in your docker container with attach command. But your docker container must be started with /bin/bash.Use below syntax to get shell access of docker container.
$ sudo docker attach <CONTAINER ID/NAME>For example, your docker container is running with id 76debad837d2 and name happy_tecadmin. Use one of the following commands with attach method.
Using Container ID:
$ sudo docker attach 76debad837d2Using Container Name:
$ sudo docker attach happy_tecadmin
#2. Using Docker Exec
If docker container was not started with /bin/bash command. Then you can’t use attach command. Now, you need to use exec command to create bash in your container. Make sure you are using Docker version >= 1.3.Use below syntax to get shell access of docker container.
$ sudo docker exec -it <CONTAINER ID/NAME> bashFor example, your docker container is running with id 76debad837d2 and name happy_tecadmin. Use one of the following commands with exec method.
Using Container ID:
$ sudo docker exec -it 76debad837d2 bashUsing Container Name:
$ sudo docker exec -it happy_tecadmin bash
How to Create, List & Delete Docker Containers on Linux

#1. Launch Docker Container
To launch a new Docker container using below command. This will start a new container and provide you access to that container with /bin/bash shell.# docker run [OPTIONS] <IMAGE NAME> [COMMAND] [ARG...] For example below command will create new docker container using the image named “ubuntu”. To list all available images use
docker images
command.# docker run -i -t ubuntu /bin/bash To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console. If you used exit command, it will stop the current container. Click here to read for more options about docker run command.
#2. List Docker Containers
After existing from Docker container, execute below command to list all running containers.# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntu By default Above command will list only running containers. To list all containers (including stopped container) use following command.
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntu 6b5b5a969241 centos "/bin/bash" 2 days ago Exited (0) 24 hours ago ubuntu-web
#3. Start/Stop/Attach Container
You can start, stop or attach to any containers with following commands. To start container use following command.# docker start <CONTAINER ID|NAME> To stop container use following command.
# docker stop <CONTAINER ID|NAME> To attach to currently running container use following command.
# docker attach <CONTAINER ID|NAME>
#4. Drop Docker Container
Before deleting any container make sure that container is stopped. You can use ‘docker ps -a’ command to list status of containers. If container is still running first stop that container using given commands in above step.Now use the following command to delete single or multiple containers.
# docker rm <CONTAINER ID|NAME> <CONTAINER ID|NAME> You can also delete all stopped containers at once using the following command.
# docker rm $(docker ps -a -q)
How to Search, Pull, List & Delete Docker Images on Linux

Search Docker Images
First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output# docker search ubuntuThe result will look like below
NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Deb... 3318 [OK] ubuntu-upstart Upstart is an e... 60 [OK] torusware/speedus-ubuntu Always updated ... 25 [OK] ubuntu-debootstrap debootstrap --v... 24 [OK] rastasheep/ubuntu-sshd Dockerized SSH ... 22 [OK] neurodebian NeuroDebian pro... 17 [OK] nuagebec/ubuntu Simple always u... 4 [OK] nickistre/ubuntu-lamp-wordpress LAMP on Ubuntu ... 4 [OK] nimmis/ubuntu This is a docke... 3 [OK]
Pull Docker Images
Now pull required docker image from docker hub on your local system using following commands. Below command will download image named “ubuntu”.# docker pull ubuntu latest: Pulling from library/ubuntu fa5be2806d4c: Pull complete b4af4261cb15: Downloading [==> ] 3.779 MB/70.55 MB 5d358abc5d9c: Download complete 2933d50b9f77: Download completeWe are also downloading centos images from docker hub.
# docker pull centos
List Docker Images
Now make sure that above images has been downloaded successfully on your system. Below command list all images.# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 36248ae4a9ac 2 hours ago 188 MB centos latest 2933d50b9f77 2 hours ago 196.6 MB
Remove Docker Images
To remove an images, Docker provides rmi option. Using this we can delete any docker images from our local system. For example use below command with changing IMAGE ID with your Docker image id.# docker rmi <IMAGE ID>or you can simply remove images using repository name (image name)
# docker rmi ubuntuIn case you have two images with same name, add tag name while deletion
# docker rmi ubuntu:latest
How to Move Docker Images between Hosts

List Docker Images
Fist list docker images on your system using below command.root@tecadmin:~# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos latest 2933d50b9f77 11 days ago 196.6 MB ubuntu latest 36248ae4a9ac 11 days ago 188 MB
Save or Archive Image
Now use the following command to save image repository named ubuntu (image id: 36248ae4a9ac) and make a zipped archive named ubuntu-latest.tar.gz. Remember that save is used for making backup of docker images (not containers).# docker save ubuntu | gzip > ubuntu-latest.tar.gz
Import Image
After saving docker image in archive format on your system move it to remote system using scp or ftp. After that use below command on remote system to import Docker image with name ubuntu and tag name latest.# zcat ubuntu-latest.gz | docker import - ubuntu:latest The above command will create a docker image with name ubuntu and tag name latest on your system. You can now launch containers using this image like below.
# docker run -i -t ubuntu /bin/bash
How to Export and Import Docker Containers

List Containers
Fist list all containers on your system using below command. Using ps -a will list all containers (running and stopped) from your system.root@tecadmin:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours ubuntu-web 2b199b9976c4 centos "/bin/bash" 3 days ago Up 3 hours centos-mysql
Export Container
Finally use the following command to export container named ubuntu-web (container id: f2582758af13) and make a zipped archive named ubuntu-web.tar.gz. Remember that export is used for making backup of docker containers (not images) in image format.# docker export ubuntu-web | gzip > ubuntu-web.tar.gz
Import Container
After exporting docker container on your system move it to remote server using scp or ftp. After that use below command on remote server to import Docker container on remote server with name ubuntu-web.# zcat ubuntu-web.gz | docker import - ubuntu-webThe above command will create a docker image on your system. You can now launch a container from this image using below command.
# docker run -i -t ubuntu-web /bin/bash
How to Remove Docker Images and Containers

Remove Docker Images
To remove an images, Docker provides rmi option. Using this we can delete any docker images from our local system. For example use below command with changing <IMAGE ID> with your Docker image id.# docker rmi <IMAGE ID> To find all images on your system use following command. This will help you to find ID of images.
root@tecadmin:~# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos latest 2933d50b9f77 11 days ago 196.6 MB ubuntu latest 36248ae4a9ac 11 days ago 188 MB
Remove Docker Containers
To remove a containers, Docker provides rm option. Using this we can delete any docker containers from our local system. For example use below command with changing<CONTAINER ID>
with your Docker container id.# docker rm <CONTAINER ID> To list all containers on your system using ps option, but ps will show only running containers. So to view all containers use -a parameter with ps.
root@tecadmin:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntu 2b199b9976c4 centos "/bin/bash" 3 days ago Up 3 hours thirsty_yalow
Stop & Remove All Docker Containers
If you want to remove all docker containers. You can use simply following commands. The first command will stop all running docker containers and the second command will delete them.Stop All Containers
# docker stop $(docker ps -a -q) Remove All Containers
# docker rm $(docker ps -a -q)
How to Install and Manage Docker Engine on CentOS/RHEL & Fedora

#1 – Verify Requirements
For standard installation Docker required 64 bit operating system having Kernel >= 3.10 version. Older versions of Kernel have some missing requirements to run all features of Docker.$ uname -r 3.19.0-49-genericAlso install the following packages on your system.
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
#2 – Add Docker Yum Repository
Let’s add the official Docker yum repository on your system.$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#3 – Install Docker
First update the yum chache for local system.$ sudo yum makecache fastNow install docker community edition package to install docker on your system. This is install many of required decencies on your system.
$ sudo yum install docker-ceAfter successful installation of Docker engine, Let’s start the docker service.
$ sudo service docker start
4. Manage Docker Container
Search Docker Images
First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output# docker search centos
Download Docker Images
Now download the Docker container with name Ubuntu on your local system using following commands.# docker pull ubuntu latest: Pulling from library/ubuntu fa5be2806d4c: Pull complete b4af4261cb15: Downloading [==> ] 3.779 MB/70.55 MB 5d358abc5d9c: Download complete 2933d50b9f77: Download completeNow make sure that above images has been downloaded successfully on your system. Below command list all images.
# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 36248ae4a9ac 2 days ago 188 MB centos latest 2933d50b9f77 2 days ago 196.6 MB
Launch New Container with Image
Finally launch a Docker container using above downloaded image on your system. Below command will start a new container and provide you access of that container with /bin/bash shell.# docker run -i -t ubuntu /bin/bashTo exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console. If you used exit command, it will stop the current container.
After existing from Docker container, execute below command to list all running containers.
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntuBy default Above command will list only running containers. To list all containers (including stopped container) use following command.
# docker ps -a
Start/Stop/Attach Container
You can start, stop or attach to any containers with following commands. To start container use following command.# docker start <CONTAINER ID>To stop container use following command.
# docker stop <CONTAINER ID>To attach to currently running container use following command.
# docker attach <CONTAINER ID>
How to Install and Manage Docker CE on Ubuntu 16.04 & 14.04 LTS

#1 – Install Requirements
First of all install the required packages for docker installation.$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-commonFor standard installation Docker required 64 bit operating system having Kernel >= 3.10 version. Older versions of Kernel have some missing requirements to run all features of Docker.
$ uname -r 4.4.0-21-genericUbuntu 14.04 LTS users recommended to install the linux-image-extra kernel package.
$ sudo apt-get install linux-image-extra-$(uname -r)
#2 – Add Docker Apt Repository
Import dockers official GPG key to verify packages signature before installing them with apt-get.$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key addRun the below comamnd to enable the docker official apt-get repository on your system. This will add entry in systems /etc/apt/sources.list file.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
#3 – Install Docker on Ubuntu
Remove the old packages installed on your system and purge them using below command.$ sudo apt-get purge docker lxc-docker docker-engineNow use the following commands to upgrade apt index and install docker engine on your system.
$ sudo apt-get update $ sudo apt-get install docker-ceAfter successful installation of Docker ce, the service will start automatically, Use below command to verify service status.
$ sudo systemctl status dockerUse our Docker Tutorial for Beginners to working with Docker.
#4 – Manage Docker Container
Search Docker Images
First of all search Docker container images from Docker hub. For example, below command will search all images with Ubuntu and list as output.# docker search ubuntu
Download Docker Images
Now download the Docker container with name Ubuntu on your local system using following commands.# docker pull ubuntu latest: Pulling from library/ubuntu fa5be2806d4c: Pull complete b4af4261cb15: Downloading [==> ] 3.779 MB/70.55 MB 5d358abc5d9c: Download complete 2933d50b9f77: Download completeNow make sure that above images has been downloaded successfully on your system. Below command list all images.
# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 36248ae4a9ac 7 days ago 188 MB
Launch New Container with Image
Finally launch a Docker container using above downloaded image on your system. Below command will start a new container and provide you access of that container with /bin/bash shell.# docker run -i -t ubuntu /bin/bashTo exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console. If you used exit command, it will stop the current container.
After existing from Docker container, execute below command to list all running containers.
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntuBy default Above command will list only running containers. To list all containers (including stopped container) use following command.
# docker ps -a
Start/Stop/Attach Container
You can start, stop or attach to any containers with following commands. To start container use following command.# docker start <CONTAINER_ID>To stop container use following command.
# docker stop <CONTAINER_ID>To attach to currently running container use following command.
# docker attach <CONTAINER_ID>
No hay comentarios:
Publicar un comentario