mirror of
				https://github.com/yeasy/docker_practice.git
				synced 2025-10-31 18:21:33 +00:00 
			
		
		
		
	Rewrite and add more details on attach and nsenter; add bashrc_docker file
This commit is contained in:
		
							
								
								
									
										45
									
								
								_local/bashrc_docker
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								_local/bashrc_docker
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| # Some useful commands to use docker. | ||||
| # Author: yeasy@github | ||||
| # Created:2014-09-25 | ||||
|  | ||||
| alias docker-pid="sudo docker inspect --format '{{.State.Pid}}'" | ||||
|  | ||||
| #the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter | ||||
| function docker-enter() { | ||||
|     if [ -e $(dirname "$0")/nsenter ]; then | ||||
|         # with boot2docker, nsenter is not in the PATH but it is in the same folder | ||||
|         NSENTER=$(dirname "$0")/nsenter | ||||
|     else | ||||
|         NSENTER=nsenter | ||||
|     fi | ||||
|     [ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return | ||||
|  | ||||
|     if [ -z "$1" ]; then | ||||
|         echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]" | ||||
|         echo "" | ||||
|         echo "Enters the Docker CONTAINER and executes the specified COMMAND." | ||||
|         echo "If COMMAND is not specified, runs an interactive shell in CONTAINER." | ||||
|     else | ||||
|         PID=$(sudo docker inspect --format "{{.State.Pid}}" "$1") | ||||
|         if [ -z "$PID" ]; then | ||||
|             echo "WARN Cannot find the given container" | ||||
|             return | ||||
|         fi | ||||
|         shift | ||||
|  | ||||
|         OPTS="--target $PID --mount --uts --ipc --net --pid" | ||||
|  | ||||
|         if [ -z "$1" ]; then | ||||
|             # No command given. | ||||
|             # Use su to clear all host environment variables except for TERM, | ||||
|             # initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH, | ||||
|             # and start a login shell. | ||||
|             #sudo $NSENTER "$OPTS" su - root | ||||
|             sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root | ||||
|         else | ||||
|             # Use env to clear all host environment variables. | ||||
|             sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@ | ||||
|         fi | ||||
|     fi | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user