如何进入码头集装箱? [英] How to get into a docker container?

查看:132
本文介绍了如何进入码头集装箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始使用Docker。我正在使用WordPress基本图像和docker-compose。我正在尝试将ssh转换为其中一个容器,以检查在初始构建期间创建的文件/目录。我试图运行 docker-compose run containername ls -la ,但没有做任何事情。即使是这样,我宁愿有一个控制台,我可以遍历目录结构,而不是运行一个命令。使用Docker做什么正确的方法?

解决方案

docker attach 将让你连接到你的Docker容器,但这并不是真的与$ code> ssh 相同。如果您的容器正在运行Web服务器,例如, docker attach 可能会将您连接到Web服务器进程的 stdout 。它不一定会给你一个shell。



Docker exec 命令可能是你正在寻找的;这将允许您在现有容器内运行任意命令。例如:

  docker exec -it< mycontainer> bash 

当然,您运行的任何命令都必须存在于容器文件系统中。



在上述命令中,< mycontainer> 是目标容器的名称或ID。无论您是否使用 docker compose ;只需运行 docker ps 并使用ID(第一列中显示的十六进制字符串)或名称(显示在最后一列中)。例如:

  $ docker ps 
d2d4a89aaee9 larsks / mini-httpdmini_httpd -d / cont 7天前up 7天web

我可以运行:

  $ docker exec -it web ip addr 
1:lo:< LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link / loopback 00: 00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8范围主机lo
valid_lft永远preferred_lft永远
inet6 :: 1 / 128 scope host
valid_lft forever preferred_lft forever
18:eth0:< BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link / ether 02:42:ac:11:00 :03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16范围全局eth0
valid_lft永远preferred_lft永远
inet6 fe80 :: 42:acff:fe11:3 / 64范围链接
valid_lft永远preferred_lft永远

我可以通过执行以下操作来完成相同的操作:

  $ docker exec -it d2d4a89aaee9 ip addr 

同样,我可以在容器中启动一个shell;

  $ docker exec -it web sh 
/#echo这是在容器内。
这是容器内。
/#exit
$


I'm getting started working with Docker. I'm using the WordPress base image and docker-compose. I'm trying to ssh into one of the containers to inspect the files/directories that were created during the initial build. I tried to run docker-compose run containername ls -la but that didn't do anything. Even if it did, I'd rather have a console where I can traverse the directory structure, rather than run a single command. What is the right way to do this with Docker?

解决方案

docker attach will let you connect to your Docker container, but this isn't really the same thing as ssh. If your container is running a webserver, for example, docker attach will probably connect you to the stdout of the web server process. It won't necessarily give you a shell.

The docker exec command is probably what you are looking for; this will let you run arbitrary commands inside an existing container. For example:

docker exec -it <mycontainer> bash

Of course, whatever command you are running must exist in the container filesystem.

In the above command <mycontainer> is the name or ID of the target container. It doesn't matter whether or not you're using docker compose; just run docker ps and use either the ID (a hexadecimal string displayed in the first column) or the name (displayed in the final column). E.g., given:

$ docker ps
d2d4a89aaee9        larsks/mini-httpd   "mini_httpd -d /cont   7 days ago          Up 7 days                               web                 

I can run:

$ docker exec -it web ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
18: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:3/64 scope link 
       valid_lft forever preferred_lft forever

I could accomplish the same thing by running:

$ docker exec -it d2d4a89aaee9 ip addr

Similarly, I could start a shell in the container;

$ docker exec -it web sh
/ # echo This is inside the container.
This is inside the container.
/ # exit
$

这篇关于如何进入码头集装箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆