[Docker] Accessing a Container

If you want to get into container and look around, you can use:

docker container exec

to run against a running container. And to do that, we need to know the id / name of the running container.

 

To get id / name of the container, we can do:

docker container ls

 

For example, the name of the container is called: elegant_noether.

docker container exec elegant_noether du -mh

And 'du -mh' will print out the size of each folder:

复制代码
$ docker container exec elegant_noether du -mh
16K     ./error/include
220K    ./error
44K     ./build
8.0K    ./htdocs
892K    ./include
72K     ./conf/extra
72K     ./conf/original/extra
96K     ./conf/original
268K    ./conf
260K    ./icons/small
1.3M    ./icons
20K     ./cgi-bin
4.5M    ./modules
8.0K    ./logs
1.2M    ./bin
8.4M    .
复制代码

 

Running commands is cool, but we can also attach a shell to those containers and browse them just like they were a full operating system.

The -i and -t flags can be passed to the exec command to keep an interactive shell open, and you can specify the shell you want to attach after the container ID. Here we'll attach a standard Bash shell.

docket container exec -it elegant_noether /bin/bash

We're in! From the perspective of the terminal window we're just navigating a normal Unix system, so any commands we type will run inside of that system. Try typing that same command we just ran with exec from inside the container.

Now to check the size of each folder, we can just do:

du -mh
复制代码
$ docker container exec -it elegant_noether /bin/bash
root@a8c267869b0b:/usr/local/apache2# du -mh
16K     ./error/include
220K    ./error
44K     ./build
8.0K    ./htdocs
892K    ./include
72K     ./conf/extra
72K     ./conf/original/extra
96K     ./conf/original
268K    ./conf
260K    ./icons/small
1.3M    ./icons
20K     ./cgi-bin
4.5M    ./modules
8.0K    ./logs
1.2M    ./bin
8.4M    .
复制代码

 

Another thing we can do once we’re in a container’s shell is install new programs, or packages, that aren’t part of the base image. Let’s install the fortunes package.

apt-get install -y fortunes

The package was installed in 

/usr/games/fortune

It’s kind of annoying to have to type /usr/games/ before the fortune command, so let’s update the PATH environment variable in the container so that we don’t have to type all of that anymore.

PATH=$PATH:/usr/games/

Now we can export path:

export PATH

After that we can just type 'fortune' to start the game

posted @   Zhentiw  阅读(270)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示