Skip to content

Instantly share code, notes, and snippets.

@dinhkk
Last active April 19, 2018 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinhkk/ecb64dfe347e1ee739303c45ed6ce6ba to your computer and use it in GitHub Desktop.
Save dinhkk/ecb64dfe347e1ee739303c45ed6ce6ba to your computer and use it in GitHub Desktop.
docker comman

How to run a docker centos 7 lastest

1 Pull an image centos7 from docker hub : https://hub.docker.com/_/centos/

docker pull centos

2 build a docker container from pulled image

docker run --privileged -d -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro  --name centos7100_7200 -p 7100-7200:7100-7200 -p 8100:80 -p 2020:22 centos  /usr/sbin/init

Comman Details:

  • centos: is the name of image that was pulled to local host

  • centos7100_7200 : is the unique container name we set when create new container

  • -p 7100-7200:7100-7200 : expose range ports 7100-7200 from container to host machine with ranges 7100-7200

  • -p 8100:80 : expose port 80 on container to host machine 8100

  • -p 2020:22 : expose port 22 on container to host machine 2020

  • --privileged -d -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro this options help us run comman systemctl successfully on container

  • command to lunch a new container and share data from host machine with guest machine

docker run --privileged -d -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v f:/www:/www --name newcentos72 -p 22:22 -p 80:80 -p 8000-8100:8000-8100 -p 9000:9000 -p 9001:9001 -p 3306:3306 centos:latest  /usr/sbin/init
docker run --privileged -d -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v e:/www:/www --name centos74php56 -p 22:22 -p 80:80 -p 443:443 -p 8000-8100:8000-8100 -p 9000:9000 -p 9001:9001 -p 3306:3306 -p 3389:3389 centos7new:newPhp7  /usr/sbin/init

-v e:/www:/www is folder from windows machine

3 Access container via comman

docker exec -ti [container_id] /usr/bin/bash

this will help us login container centos7 via ssh.

4 now on conainer ssh screen , we setup httpd service with root account:

yum update
yum install httpd
systemctl start httpd

now run browser on localhost : http://localhost-ip:8100 we see the welcome page of apache2

5 login via ssh to container

  • on container comman change root password
passwd root
  • on host run command
ssh root@localhost-ip -p 2020

Enter your password, then we will login to centos7 container.

6. copy a running system from a container, and restore a copied image

docker export centos7new > centos7new-copy.tar

centos7new : name of current container

docker import centos7new-copy.tar centos7new:newPhp7

centos7new:newPhp7 : [IMAGENAME] : [NEWTAGE]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment